package com.hh.item.dao;

import com.hh.item.pojo.Message;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

/**
 * @author yangfan
 * @date 2020/3/8 19:17
 * @description
 */
@Repository("messageDao")
@Mapper
public interface IMessageDao extends tk.mybatis.mapper.common.Mapper<Message> {

    @Select("SELECT u.user_id,u.user_name,u.user_avatar,m.msg_id,m.msg_content,m.msg_collect,m.msg_like,m.msg_time FROM user_info u " +
            "RIGHT JOIN msg_info m ON u.user_id = m.user_id " +
            "WHERE m.user_id IN " +
            "( SELECT target_id FROM user_relation WHERE relation_type = 1 AND user_id = #{userId} ) " +
            "OR m.user_id = #{userId} ORDER BY msg_time DESC")
    @Results(id = "showFollowMsg", value = {
            @Result(column = "user_id", property = "user.userId"),
            @Result(column = "user_name", property = "user.userName"),
            @Result(column = "user_avatar", property = "user.userAvatar"),
            @Result(column = "msg_id", property = "msgId"),
            @Result(column = "msg_content", property = "msgContent"),
            @Result(column = "msg_collect", property = "msgCollect"),
            @Result(column = "msg_like", property = "msgLike"),
            @Result(column = "msg_time", property = "msgTime")
    })
    List<Message> mainMsgs(Integer userId);

    @Select("select count(*) from msg_info where user_id=#{userId}")
    Integer selectMsgCountByUserId(Integer userId);

    @Select("select * from msg_info where user_id=#{userId} ORDER BY msg_time DESC")
    @Results(id = "oneMsg", value = {
            @Result(column = "msg_id", property = "msgId"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "msg_image", property = "msgImage"),
            @Result(column = "msg_content", property = "msgContent"),
            @Result(column = "msg_collect", property = "msgCollect"),
            @Result(column = "msg_like", property = "msgLike"),
            @Result(column = "msg_time", property = "msgTime")
    })
    List<Message> getMsgs(Integer userId);

    @Select("select relation_type from user_msg_index where user_id=#{userId} and msg_id=#{msgId}")
    List<Integer> getRelationStatus(@Param("userId") Integer userId,@Param("msgId") Integer msgId);

    @Update("update msg_info set msg_like=msg_like+1 where msg_id = #{msgId}")
    int addLike(Integer msgId);

    @Update("update msg_info set msg_like=msg_like-1 where msg_id = #{msgId}")
    int cancelLike(Integer msgId);

    @Update("update msg_info set msg_collect=msg_collect+1 where msg_id = #{msgId}")
    int addCollect(Integer msgId);

    @Update("update msg_info set msg_collect=msg_collect-1 where msg_id = #{msgId}")
    int cancelCollect(Integer msgId);

    @Insert("insert into user_msg_index(user_id,msg_id,relation_type) values (#{userId},#{msgId},1)")
    int like(@Param("userId") Integer userId,@Param("msgId") Integer msgId);

    @Delete("delete from user_msg_index where user_id=#{userId} and msg_id=#{msgId} and relation_type=1")
    int unLike(@Param("userId") Integer userId,@Param("msgId") Integer msgId);

    @Insert("insert into user_msg_index(user_id,msg_id,relation_type) values (#{userId},#{msgId},0)")
    int Collect(@Param("userId") Integer userId,@Param("msgId") Integer msgId);

    @Delete("delete from user_msg_index where user_id=#{userId} and msg_id=#{msgId} and relation_type=0")
    int unCollect(@Param("userId") Integer userId,@Param("msgId") Integer msgId);

    @Select("SELECT m.msg_id, m.user_id, u.user_name, m.msg_content  FROM msg_info m, user_info u  WHERE m.msg_id IN ( SELECT msg_id FROM user_msg_index WHERE user_id = #{userId} AND relation_type = 0 ) AND m.user_id = u.user_id;")
    @Results(id = "showCollectMsg", value = {
            @Result(column = "msg_id", property = "msgId"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "user_name", property = "user.userName"),
            @Result(column = "msg_content", property = "msgContent"),

    })
    List<Message> collectMsgs(Integer userId);










    /*@Insert("insert into msg_info(user_id,msg_type,msg_content,msg_image,msg_comment,msg_transfer,msg_time)values(#{userId},#{msgType},#{msgContent},#{msgImage},#{msgComment},#{msgTransfer},#{msgTime})")
    int save(Message message);*/


}
